﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace ArgusPCLib.IO
{
	public class StreamHub : Stream
	{
		List<Stream> streams;

		public StreamHub(IEnumerable<Stream> streams)
		{
			if (streams == null)
				this.streams = new List<Stream>();
			else
				this.streams = new List<Stream>(streams);
		}

		public StreamHub()
			: this(null)
		{
		}

		public void AddStream(Stream stream)
		{
			if (this.streams.Contains(stream) == false)
				this.streams.Add(stream);
		}

		public bool RemoveStream(Stream stream)
		{
			return this.streams.Remove(stream);
		}

		public override bool CanRead { get { return false; } }

		public override bool CanSeek
		{
			get { return false; }
		}

		public override bool CanTimeout
		{
			get
			{
				foreach (Stream stream in this.streams)
				{
					if (stream.CanTimeout == true)
						return true;
				}
				return false;
			}
		}

		public override bool CanWrite
		{
			get
			{
				foreach (Stream stream in this.streams)
				{
					if (stream.CanWrite == false)
						return false;
				}
				return true;
			}
		}

		public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
		{
			foreach (Stream stream in this.streams)
			{
				await stream.WriteAsync(buffer, offset, count, cancellationToken);
			}
		}

		public override void Write(byte[] buffer, int offset, int count)
		{
			foreach (Stream stream in this.streams)
			{
				stream.Write(buffer, offset, count);
			}
		}

		public override int Read(byte[] buffer, int offset, int count)
		{
			throw new NotImplementedException();
		}

		public override void SetLength(long value)
		{
			foreach (Stream stream in this.streams)
			{
				stream.SetLength(value);
			}
		}

		public override long Seek(long offset, SeekOrigin origin)
		{
			throw new NotImplementedException();
		}

		public override void Flush()
		{
			foreach (Stream stream in this.streams)
			{
				stream.Flush();
			}
		}

		public override long Position
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		public override long Length
		{
			get { throw new NotImplementedException(); }
		}
	}
}
